﻿Imports System
Imports System.Math
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Data

Module XMLValidation

    Private sourceFileLocation As String = "C:\NAS_msd\Import_Package\Xml_for_import\100025_1005_16Jul2008.xml"
    Private validationSettings As XmlReaderSettings
    Private IsSchemaValidated As Boolean = True
    Private schemaValidationMessage As String
    Dim ds As New DataSet
    Dim FileName As String
    Dim DEAPVersion As String
    Dim _CurrentDEAPVersion As String = "3.2.0,3.2.1"
    Dim _DEAPVersion As String = ""
    Dim _IsCurrentVersion As Boolean = False
    Dim _XMLSchema As String = "C:\NAS_msd\DEAPv320.xsd"
    Dim _LegacyXMLSchema As String = "C:\NAS_msd\DataImport.xsd"
    Dim _ValidationOutcome As Boolean = False

    Sub Main()
        Try
            Dim delimiter As String = ","
            Dim IsCurrentDEAPVersion As Boolean
            FileName = "C:\NAS_msd\Import_Package\Xml_for_import\100025_1005_16Jul2008.xml"
            ds.ReadXml(FileName)
            DEAPVersion = ds.Tables("DwellingData").Rows(0)("DEAPVersion").ToString
            _DEAPVersion = DEAPVersion
            Dim CurrentDEAPVersion As String = _CurrentDEAPVersion
            Dim CurrentDEAPVersions() As String = CurrentDEAPVersion.Split(New String() {delimiter}, StringSplitOptions.RemoveEmptyEntries)
            SetFilePaths()
            For Each item As String In CurrentDEAPVersions
                If DEAPVersion = item And IsCurrentDEAPVersion = False Then
                    IsCurrentDEAPVersion = True
                End If
            Next

            _IsCurrentVersion = IsCurrentDEAPVersion

            If IsCurrentDEAPVersion Then
                InitializeXmlReaderSettings(_XMLSchema)
            Else
                InitializeXmlReaderSettings(_LegacyXMLSchema)
            End If

            ValidateSchema(sourceFileLocation)
            ReportValidationAttempt()
        Catch ex As Exception
            Console.WriteLine(ex.Message.ToString())
        End Try
    End Sub

    Private Sub SetFilePaths()
        'sourceFileLocation = Dts.Connections("XML File").AcquireConnection(Nothing).ToString()
    End Sub

    'Initialize Reader settings to check for conformant XML
    Private Sub InitializeXmlReaderSettings(ByVal xsdLocation As String)
        validationSettings = New XmlReaderSettings()
        validationSettings.ValidationType = ValidationType.Schema
        validationSettings.Schemas.Add(Nothing, xsdLocation)
        validationSettings.Schemas.Compile()
        AddHandler validationSettings.ValidationEventHandler, AddressOf ValidationCallBack

    End Sub

    ' validates schema
    Private Sub ValidateSchema(ByVal xmlDocumentLocation As String)
        Using reader As XmlReader = XmlReader.Create(xmlDocumentLocation, validationSettings)
            While (reader.Read())
            End While
        End Using
    End Sub

    Private Sub ReportValidationAttempt()
        If IsSchemaValidated Then
            _ValidationOutcome = True
        Else
            _ValidationOutcome = False
        End If
    End Sub

    ' XSD validation callback
    Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As ValidationEventArgs)
        IsSchemaValidated = False
        schemaValidationMessage = args.Message
    End Sub

End Module
